package com.jeesite.modules.tk.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.ic.entity.IcUser;
import com.jeesite.modules.ic.service.IcUserService;
import com.jeesite.modules.tk.entity.TkTask;
import com.jeesite.modules.tk.service.TkTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.tk.entity.TkJubao;
import com.jeesite.modules.tk.service.TkJubaoService;

/**
 * 举报悬赏Controller
 * @author 李寒冰
 * @version 2025-05-09
 */
@Controller
@RequestMapping(value = "${adminPath}/tk/tkJubao")
public class TkJubaoController extends BaseController {

	@Autowired
	private TkJubaoService tkJubaoService;
    @Autowired
    private IcUserService icUserService;
    @Autowired
    private TkTaskService tkTaskService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TkJubao get(String id, boolean isNewRecord) {
		return tkJubaoService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("tk:tkJubao:view")
	@RequestMapping(value = {"list", ""})
	public String list(TkJubao tkJubao, Model model) {
		model.addAttribute("tkJubao", tkJubao);
		return "modules/tk/tkJubaoList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("tk:tkJubao:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TkJubao> listData(TkJubao tkJubao, HttpServletRequest request, HttpServletResponse response) {
		tkJubao.setPage(new Page<>(request, response));
		Page<TkJubao> page = tkJubaoService.findPage(tkJubao);
		List<TkJubao> list = page.getList();
		for (TkJubao tkJubao1 : list) {
			String userId = tkJubao1.getUserId();
			IcUser icUser = icUserService.get(userId);
			tkJubao1.setIcUser(icUser);
			TkTask tkTask = tkTaskService.get(tkJubao1.getTaskId());
			tkJubao1.setTkTask(tkTask);
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("tk:tkJubao:view")
	@RequestMapping(value = "form")
	public String form(TkJubao tkJubao, Model model) {
		model.addAttribute("tkJubao", tkJubao);
		return "modules/tk/tkJubaoForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("tk:tkJubao:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TkJubao tkJubao) {
		tkJubaoService.save(tkJubao);
		return renderResult(Global.TRUE, text("保存举报悬赏成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("tk:tkJubao:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TkJubao tkJubao) {
		tkJubaoService.delete(tkJubao);
		return renderResult(Global.TRUE, text("删除举报悬赏成功！"));
	}
	
}